import router from 'router/index' // 路由
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'
import { getToken } from '@/common/auth' // get token from cookie
import store from './store'
const whiteList = ['/login'] // 白名单

router.beforeEach(async(to, from, next) => {
  // 动态设置公用组件菜单状态
  if (to.meta.common && from.meta.hasCommon) {
    to.meta.parentName = from.name
  } else if (to.meta.common) {
    to.meta.parentName = from.meta.parentName
  }

  NProgress.start()

  const hasToken = getToken()
   // 如果self==top，则证明不是来自iframe
   if (self == top) {
    // if (hasToken) {
    //   // 有token
    //   next()
    // } else {
    //   // 没有token
    //   if (whiteList.indexOf(to.path) !== -1) {
    //     next()
    //   } else {
    //     next('/login')
    //     NProgress.done()
    //   }
    // }
    next()
  } else { //来自iframe
    function getQueryVariable(variable) { // 获取参数方法
      var query = window.location.hash.substring(1);
      console.log( window.location,'@@@')
      console.log( query,'##')
      var vars = query.split("?");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
      }
      return (false);
    }
    let dep_code = getQueryVariable('dep_code')
    console.log(dep_code,'dep_code')
    store.commit('DEP_CODE', dep_code);//参数存到vuex
    next()
  }
})

// 进度条结束
router.afterEach(transition => {
  NProgress.done()
})
